<?php

//记录日志
function logger($msg) {
	$line = '[ ' . date('Y-m-d H:i:s') . ' ]' . $msg . "\n\n";
	file_put_contents('phpout.log', $line, FILE_APPEND);
	echo $line;
}

//推送消息
function easyPushWechatMsg($msg) {
	$postData = json_encode([
		'appToken' => 'AT_XGmRz8RFSZBtLyaYBx52r8YziWCSrbEg',
		'content' => $msg,
		'contentType' => 1,
		'uids' => [
			'UID_ICAsgfMbgOLiDal8Zs6IYs3cF1pn',
			'UID_n1quSUwrZ0QRNkITxOeH54bYSEbK',
			'UID_e2EvnkezKPxdnQvDBPv11sdWYno3',
			'UID_wWE8tvSSFX8ZLK0F6FMFRLpD82cB',
			'UID_iIMR2zTBg42poNmVq9O2PAsA7v1V',
		],
	]);
	$cmdStr = 'curl \\
    -H "Content-type: application/json" \\
    -d \'' . $postData . '\' \\
    http://wxpusher.zjiecode.com/api/send/message';
	exec($cmdStr, $output);
	print_r($output);
}

function getOpn($cookie, $proxy) {
	$cmdStr = 'curl ' . $proxy . ' \'https://www.ti.com.cn/storeservices/cart/opninventory?opn=LM25575MHX/NOPB\' \
      -H \'authority: www.ti.com.cn\' \
      -H \'pragma: no-cache\' \
      -H \'cache-control: no-cache\' \
      -H \'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"\' \
      -H \'sec-ch-ua-mobile: ?0\' \
      -H \'sec-ch-ua-platform: "macOS"\' \
      -H \'upgrade-insecure-requests: 1\' \
      -H \'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36\' \
      -H \'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\' \
      -H \'sec-fetch-site: none\' \
      -H \'sec-fetch-mode: navigate\' \
      -H \'sec-fetch-user: ?1\' \
      -H \'sec-fetch-dest: document\' \
      -H \'accept-language: zh-CN,zh;q=0.9\' \
      -H \'cookie: ' . $cookie . '\' \
      --compressed';

	exec($cmdStr, $output);

	return $output;
}

function getCookie($cookie, $proxy) {
	$cmdStr = 'curl -I ' . $proxy . ' \'https://www.ti.com.cn/storeservices/cart/opninventory?opn=LM25575MHX/NOPB\' \
      -H \'authority: www.ti.com.cn\' \
      -H \'pragma: no-cache\' \
      -H \'cache-control: no-cache\' \
      -H \'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"\' \
      -H \'sec-ch-ua-mobile: ?0\' \
      -H \'sec-ch-ua-platform: "macOS"\' \
      -H \'upgrade-insecure-requests: 1\' \
      -H \'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36\' \
      -H \'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\' \
      -H \'sec-fetch-site: none\' \
      -H \'sec-fetch-mode: navigate\' \
      -H \'sec-fetch-user: ?1\' \
      -H \'sec-fetch-dest: document\' \
      -H \'accept-language: zh-CN,zh;q=0.9\' \
      -H \'cookie: ' . $cookie . '\' \
      --compressed';

	exec($cmdStr, $output);

	return $output;
}

$cookie = '';

$proxy = '--proxy 118.123.43.17:64256';

$is_get_opn = 0;

while (1) {
	// $redis = new Redis();
	// $redis->connect('sh-crs-r28csn61.sql.tencentcdb.com', 25772);
	// $redis->auth('Lx231404');

	// //检测是否有任务
	// $newSerial = $redis->get('ti_worker_order');
	// if (empty($newSerial)) {
	//     logger('无任务...');
	//     sleep(3);
	//     continue;
	// }

	// $cookie = $redis->get('ti_worker_cookie');
	// $redis->close();
	// if (empty($cookie)) {
	//     logger("从redis读取cookie失败");
	//     sleep(3);
	//     continue;
	// }

	//在第3步，获取CSRFToken

	if ($is_get_opn == 0) {
		$output = getCookie($cookie, $proxy);

		//var_dump($output);

		foreach ($output as $line) {
			if (strstr($line, 'set-cookie:')) {
				$tmp = explode("set-cookie:", $line);

				if ($tmp[1]) {
					$tmp2 = explode("path=", $tmp[1]);

					$tmp3 = explode("Domain=", $tmp2[0]);
					$cookie .= $tmp3[0];

				}
			}
		}
	}

	$result = getOpn($cookie, $proxy);

	echo "==========";

	if (strstr($result[0], 'orderable_number')) {
		$is_get_opn = 1;
		echo ($result[0]);
		//exit;
	} else {
		$is_get_opn = 0;
	}

	// $found = 0;
	// foreach ($output as $line) {
	//     if (preg_match('|ACC.config.CSRFToken = "([0-9a-zA-Z-]+)"|i', $line, $matches)) {
	//         if ($matches && !empty($matches[1])) {
	//             $found = 1;
	//             $CSRFToken = $matches[1];
	//         }
	//     }
	// }
	// if ($found) {
	//     logger('获取CSRFToken成功,'.$CSRFToken);
	// } else {
	//     logger("获取CSRFToken失败！\n".print_r($output, true));
	//     sleep(3);
	//     continue;
	// }
	// //$CSRFToken = 'b9812d3e-c167-400a-82a0-e19b20b1fba0';

	// //合规确认
	// $getData = http_build_query([
	//     'militaryFlag' => 'No',
	//     'eeId' => 1090,
	//     'eeName' => '单板计算机',
	//     'appId' => 892,
	//     'appName' => '工厂自动化与控制',
	//     'selectedIOR' => '',
	// ]);
	// $cmdStr = 'curl --compressed \
	// -H \'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\' \\
	// -H \'Accept-Encoding: gzip, deflate, br\' \\
	// -H \'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8\' \\
	// -H \'Cache-Control: no-cache\' \\
	// -H \'Connection: keep-alive\' \\
	// -H \'Cookie: '.$cookie.'\' \\
	// -H \'Host: www.ti.com.cn\' \\
	// -H \'Pragma: no-cache\' \\
	// -H \'Referer: https://www.ti.com.cn/store/ti/zh/checkout/buy/multi/regulations-step/choose\' \\
	// -H \'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"\' \\
	// -H \'sec-ch-ua-mobile: ?0\' \\
	// -H \'sec-ch-ua-platform: "macOS"\' \\
	// -H \'Sec-Fetch-Dest: document\' \\
	// -H \'Sec-Fetch-Mode: navigate\' \\
	// -H \'Sec-Fetch-Site: same-origin\' \\
	// -H \'Sec-Fetch-User: ?1\' \\
	// -H \'Upgrade-Insecure-Requests: 1\' \\
	// -H \'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36\' \\
	// "https://www.ti.com.cn/store/ti/zh/checkout/buy/multi/regulations-step/validate?'.$getData.'"';
	// exec($cmdStr, $output);
	// logger('合规确认'.print_r($output, true));

	// //确认运费
	// $cmdStr = 'curl --compressed \
	// -H \'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\' \\
	// -H \'Accept-Encoding: gzip, deflate, br\' \\
	// -H \'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8\' \\
	// -H \'Cache-Control: no-cache\' \\
	// -H \'Connection: keep-alive\' \\
	// -H \'Cookie: '.$cookie.'\' \\
	// -H \'Host: www.ti.com.cn\' \\
	// -H \'Pragma: no-cache\' \\
	// -H \'Referer: https://www.ti.com.cn/store/ti/zh/checkout/buy/multi/delivery-method/choose\' \\
	// -H \'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"\' \\
	// -H \'sec-ch-ua-mobile: ?0\' \\
	// -H \'sec-ch-ua-platform: "macOS"\' \\
	// -H \'Sec-Fetch-Dest: document\' \\
	// -H \'Sec-Fetch-Mode: navigate\' \\
	// -H \'Sec-Fetch-Site: same-origin\' \\
	// -H \'Sec-Fetch-User: ?1\' \\
	// -H \'Upgrade-Insecure-Requests: 1\' \\
	// -H \'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36\' \\
	// "https://www.ti.com.cn/store/ti/zh/checkout/buy/multi/delivery-method/select?delivery_method=v2-china-DOM-0_1k-EH&termsAccepted=Yes"';
	// exec($cmdStr, $output);
	// logger('运费确认'.print_r($output, true));

	// //提交订单
	// $step5Data = 'paymentMethod=unionpay_b&CSRFToken='.$CSRFToken;
	// $cmdStr = 'curl --compressed \
	// -H \'Accept: */*\' \\
	// -H \'Accept-Encoding: gzip, deflate, br\' \\
	// -H \'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8\' \\
	// -H \'Cache-Control: no-store, must-revalidate\' \\
	// -H \'Connection: keep-alive\' \\
	// -H \'Expires: 0\' \\
	// -H \'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\' \\
	// -H \'X-Sec-Clge-Req-Type: ajax\' \\
	// -H \'Cookie: '.$cookie.'\' \\
	// -H \'Host: www.ti.com.cn\' \\
	// -H \'Referer: https://www.ti.com.cn/store/ti/zh/checkout/buy/multi/payment-method/add\' \\
	// -H \'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"\' \\
	// -H \'sec-ch-ua-mobile: ?0\' \\
	// -H \'sec-ch-ua-platform: "macOS"\' \\
	// -H \'Sec-Fetch-Dest: empty\' \\
	// -H \'Sec-Fetch-Mode: cors\' \\
	// -H \'Sec-Fetch-Site: same-origin\' \\
	// -H \'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36\' \\
	// -d \''.$step5Data.'\' \\
	// https://www.ti.com.cn/store/ti/zh/checkout/buy/multi/payment-method/citcon/addSelectedPayment';
	// exec($cmdStr, $output);
	// //检查是否成功
	// $found = 0;
	// foreach ($output as $line) {
	//     if (strpos($line, '您的订单号是') !== false) {
	//         if (preg_match('|T\d{5,}|', $line, $matches) && !empty($matches)) {
	//             $orderNo = $matches[0];
	//             $found = 1;
	//         }
	//     }
	// }
	// if ($found) {
	//     $msg = '下单成功,'.$orderNo;
	//     logger($msg);
	//     easyPushWechatMsg($msg);
	// } else {
	//     $msg = "下单失败\n".print_r($output, true);
	//     logger($msg);
	//     easyPushWechatMsg('下单失败');
	// }
	sleep(2);
}
